/**
 * Created by adrain on 2016/3/2.
 */
;(function($){
    //写一个千位分符玩一下
    function toThousands(num){
        var num = (num || 0).toString(),
            result='',
            quantile = '';
        if(num.indexOf('.')>0){
            quantile = num.slice(num.indexOf('.'),num.length);
            num = num.slice(0,num.indexOf('.'));
        }
        while (num.length > 3){
            result = ',' + num.slice(-3) + result;
            num = num.slice(0,num.length-3);
        }
        if(num){
            result = num + result + quantile;
        }
        return result;
    }
    //网银支付和快捷支付选项卡切换事件
    /*
     $('#rechagePaymentUL > li').on('click',function(){
     //缓存当前元素，以及内容容器集合元素
     var $that = $(this),
     $itmes = $(this).parent().siblings('.mode-item');

     //缓存当前点击元素在li中的索引位置。
     var index = $that.index();

     //切换显示内容
     $itmes.hide().eq(index).show();
     //给选项卡加效果
     $that.addClass('active').siblings().removeClass('active');
     return false;
     });
     */
    //银行支付页面 充值银行列表选择事件
    /*
     $("#banks > .bank").on("click", function(e) {
     //缓存银行支付额度表格元素集.
     var $that = $(this),
     $banks = $("#bank_collections > .center");

     //隐藏所有的支付额度表格，筛选出当前单选项匹配的，并显示出来.
     if($banks){
     $banks.hide().filter("[data-name='" + ($that.attr("data-name")) + "']").show();
     }

     //给选中的银行加效果.
     $that.addClass("active").siblings().removeClass("active");

     $('#ceshi').on('click',function(){
     if($that.hasClass('active')){
     alert($that.attr('data-name'));
     }
     });
     });
     */

    //快捷支付是否选中状态切换
    /*$('#quickBankBox').on('click',function(){
     $(this).hasClass('active') ? $(this).removeClass('active') : $(this).addClass('active');
     });*/
    //关闭弹层单击函数
    function winCloseAll() {
        $(".popup-mask, .popup").hide();
        return false;
    }
    //关闭按钮单击事件.
    function btnClosePopup() {
        $(".popup").find("[data-action='close']").on("click", function() {
            //关闭自己的弹窗层和遮罩层.
            winCloseAll();
            return false;
        });
    }
    btnClosePopup();


    //写一个通用的选项卡切换呢
    $('#tabTit').children().on('click',function(){
        //缓存当前元素，以及内容容器集合元素
        var $that = $(this),
            $itmes = $('#tabItem').children('.item');

        //缓存当前点击元素在li中的索引位置。
        var index = $that.index();

        //切换显示内容
        $itmes.hide().eq(index).show();
        //给选项卡加效果
        $that.addClass('active').siblings().removeClass('active');
        return false;
    });

    //点击眼睛图标显示或隐藏密码
    $('#passwordEye').on('click',function(){
        var $that = $(this);
        if($('#password').attr('type') == 'password'){
            $that.removeClass('password-eye-close');
            $('#password').attr('type','text');
        }else if($('#password').attr('type') == 'text'){
            $that.addClass('password-eye-close');
            $('#password').attr('type','password');
        }
    });
    //上传包装file
    //$('input[id=lefile]').change(function(){
    //    $('#photoCover').val($(this).val());
    /*
     ps:html代码
     <input id="lefile" type="file" style="display: none;">
     <div class="input-append">
     <input id="photoCover" class="input-large" type="text">
     <a class="btn btn-default" onclick="$('input[id=lefile]').click();">选择图片</a>
     </div>
     */
    //});
    //加入我们和帮助中心的标题内容显示隐藏
    var ahTabDlts = $('#ahTabDl').find('dt');
    //ahTabDlts.eq(0).next().show();
    ahTabDlts.each(function(){
        $(this).click(function(){
            if( $(this).next().is(':hidden')){
                $(this).addClass('active').next().slideDown();
            }else{
                $(this).removeClass('active').next().slideUp();
            }
        });
    });
    //滚动的时候才显示向上回到顶部箭头
    /*$(window).scroll(function(){
     if ($(window).scrollTop()>100){
     $("#rightDownSlide > .arrowtop").show();
     }
     else
     {
     $("#rightDownSlide > .arrowtop").hide();
     }
     });*/
    //当点击跳转链接后，回到页面顶部位置
    $("#rightDownSlide > .arrowtop").on('click',function(){
        $('body,html').animate({scrollTop:0},1000);
        return false;
    });

    //找到滑过文字提示
    var dataTips = $('[data-tip]');
    dataTips.hover(function(){
        $(this).html('');
        var dataTipHtml = $.trim($(this).html()) ;//找到当前显示利率
        var dataTipDivCon = '<div class="tooltip">' + //滑过要加显示的东东
            $(this).attr('data-tip')+
            '</div>';
        if($(this).closest('li')){
            $(this).closest('li').css({
                'z-index':2
            });
        };
        $(this).html(dataTipHtml+dataTipDivCon);

    },function(){
        //var dataTipHtml = $(this).find('.tooltip span').html();

        if($(this).closest('li')){
            $(this).closest('li').css({
                'z-index':1
            });
        };
        $(this).html('');
    });



})(jQuery);

//封装一个点击弹层的函数
function clickPopup(id,openId,callback) {
    //id为需要点击的id，openId为弹层的id;
    $(id).on('click',function () {
        clickPopupOpen(openId)
        if($.isFunction(callback)){
            callback();
            return false;
        }
        popupHeightCenter($(openId));
        return false;
    });
}
function clickPopupOpen(id) {
    $('.popup-mask').show();
    $(id).fadeIn();
}
//封装一个弹层垂直居中函数
function popupHeightCenter(popup) {
    var winH = $(window).height();
    var popH = popup.outerHeight();
    //console.info(winH,popH);
    if(popH>winH){
        return false;
    }else {
        popup.css('position', 'fixed').animate({
            'top' : (winH/2-popH/2) + 'px'
        })
    }
}

//关闭弹层单击函数
function winCloseAll2() {
    $(".popup-mask, .popup").hide();
    return false;
}
//关闭按钮单击事件.
function btnClosePopup2() {
    $(".popup").find("[data-action='close']").on("click", function() {
        //关闭自己的弹窗层和遮罩层.
        winCloseAll2();
        return false;
    });
}

var popupNum =0;//声明一个弹层变量用来判断是否有这个弹层

//判断页面是否有popup-mask，有则显示无则加入到body
function popupMask() {
    $('.popup-mask').show();
    return false;
}

var samllIconPopupBodyNum =0;

//封装一个极简点击操作状态带icon的函数(比如已复制，已经修改等)
function samllIconPopupState(statename,conText) {
    samllIconPopupBodyNum+=1;
    var popupBody = '';
    popupBody = '<div class="popup smallPopup" data-after="'+statename+'" style="display: block;">'+
        '<div class="popup-tit"><span>操作提示</span>'+
        '<a class="icon-colseBox" data-action="close"><i class="icon16 icon-colse"></i></a></div>'+
        '<div class="popup-body">'+
        '<div class="popup-bodyText">'+
        '<div class="suceess-box"><span class="icon64-relevant icon64-suceess"></span>'+
        '<div class="describe-icon">'+conText+'</div>'+
        '</div></div>'+
        '<div class="popup-bodyBtn">'+
        '<a class="btn btn-line" data-action="close">确定</a>'+
        '</div></div></div>';

    if(samllIconPopupBodyNum>1){
        samllIconPopupBodyNum=1;
        if($('[data-after='+statename+']').length==0){
            $('body').append(popupBody);
            popupMask();
            btnClosePopup2();
            return false;
        }
        $('[data-after='+statename+']').show();
        popupMask();
        btnClosePopup2();
        return false;
    }
    $('body').append(popupBody);
    popupMask();
    btnClosePopup2();
}

//纯文字操作提示弹层
function textPopupState(tit,context,btnText,callback,btnColse,noColseIco) {
    $('.popupBox').html('');
    var bigTit = '',
        btnHtml='',
        smallTitHtml = '',
        noColseHtml = '';

    if(tit=='操作提示'){
        bigTit=tit;
        smallTitHtml='<div class="popup-body-smallText">'+context+'</div>';
    }else{
        bigTit = '操作提示';
        smallTitHtml = '<div class="popup-body-smallTit">'+tit+'</div>'+
            '<div class="popup-body-smallText">'+context+'</div>';
    }
    if(btnText == undefined){
        btnHtml='<a class="btn btn-line" data-action="close">确定</a>'
    }else if(typeof (btnText) == 'string'){
        btnHtml='<a class="btn btn-line" data-action="close">'+btnText+'</a>';
    }else if($.isArray(btnText)){
        btnHtml = '<a class="btn btn-line" data-action="close">'+btnText[0]+'</a>'+
            '<a class="btn btn-default2" >'+btnText[1]+'</a>';
    };

    if(!noColseIco){
        noColseHtml = '<a class="icon-colseBox" data-action="close"><i class="icon16 icon-colse"></i></a>';
    }else {
        noColseHtml = '';
    }

    var popupBody = '';
    popupBody = '<div class="popup smallPopup" style="display: block;">' +
        '<div class="popup-tit"><span>'+bigTit+'</span>'+ noColseHtml +
        '</div>'+
        '<div class="popup-body">'+
        '<div class="popup-bodyText">'+smallTitHtml+
        '</div>'+
        '<div class="popup-bodyBtn">'+btnHtml+
        '</div></div></div>';
    $('.popupBox').html(popupBody);
    popupMask();
    btnClosePopup2();
    //点击确定执行callback
    $('.btn-default2').on('click',function () {
        callBackFun();
        return false;

    });
    if(btnColse){
        $("[data-action='close']").on('click',function () {
            callBackFun();
            return false;
        });
    }
    function callBackFun() {
        if($.isFunction(callback)){
            $('.popupBox').html('');
            $('.popup-mask').hide();
            callback();
        }
    }
    return false;

}



//纯文字操作提示弹层<点击返回按钮触发函数>
function textPopupState2(tit,context,btnText,callback,btnColse,noColseIco) {
    $('.popupBox').html('');
    var bigTit = '',
        btnHtml='',
        smallTitHtml = '',
        noColseHtml = '';

    if(tit=='操作提示'){
        bigTit=tit;
        smallTitHtml='<div class="popup-body-smallText">'+context+'</div>';
    }else{
        bigTit = '操作提示';
        smallTitHtml = '<div class="popup-body-smallTit">'+tit+'</div>'+
            '<div class="popup-body-smallText">'+context+'</div>';
    }
    if(btnText == undefined){
        btnHtml='<a class="btn btn-line" data-action="close">确定</a>'
    }else if(typeof (btnText) == 'string'){
        btnHtml='<a class="btn btn-line" data-action="close">'+btnText+'</a>';
    }else if($.isArray(btnText)){
        btnHtml = '<a class="btn btn-line" data-action="close">'+btnText[0]+'</a>'+
            '<a class="btn btn-default2" >'+btnText[1]+'</a>';
    };

    if(!noColseIco){
        noColseHtml = '<a class="icon-colseBox" data-action="close"><i class="icon16 icon-colse"></i></a>';
    }else {
        noColseHtml = '';
    }

    var popupBody = '';
    popupBody = '<div class="popup smallPopup" style="display: block;">' + noColseHtml +
        '<div class="popup-tit"><span>'+bigTit+'</span>'+
        '</div>'+
        '<div class="popup-body">'+
        '<div class="popup-bodyText">'+smallTitHtml+
        '</div>'+
        '<div class="popup-bodyBtn">'+btnHtml+
        '</div></div></div>';
    $('.popupBox').html(popupBody);
    popupMask();
    btnClosePopup2();
    //点击确定执行callback
    $('.btn-default2').on('click',function () {
        //callBackFun();

        if($.isFunction(callback)){
            $('.popupBox').html('');
            $('.popup-mask').hide();
            //callback();
        }
        return false;

    });
    if(!btnColse){
        $("[data-action='close']").on('click',function () {
            callBackFun();
            return false;
        });
    }
    function callBackFun() {
        if($.isFunction(callback)){
            $('.popupBox').html('');
            $('.popup-mask').hide();
            callback();
        }
    }
    return false;

}

//自定义大标题文字操作提示弹层
function titPopupState(tit,context,btnText,callback,btnColse) {
    $('.popupBox').html('');
    var bigTit = '',
        btnHtml='',
        smallTitHtml = '';

    (tit!='') ? bigTit=tit : bigTit = '操作提示';
    smallTitHtml='<div class="popup-body-smallText">'+context+'</div>';

    if(btnText == undefined){
        btnHtml=''
    }else if(typeof (btnText) == 'string'){
        btnHtml='<div class="popup-bodyBtn"><a class="btn btn-line" data-action="close">'+btnText+'</a></div>';
    }else if($.isArray(btnText)){
        btnHtml = '<div class="popup-bodyBtn"><a class="btn btn-line" data-action="close">'+btnText[0]+'</a>'+
            '<a class="btn btn-default2" >'+btnText[1]+'</a></div>';
    };

    var popupBody = '';
    popupBody = '<div class="popup smallPopup" style="display: block;">'+
        '<div class="popup-tit"><span>'+bigTit+'</span>'+
        '<a class="icon-colseBox" data-action="close"><i class="icon16 icon-colse"></i></a></div>'+
        '<div class="popup-body">'+
        '<div class="popup-bodyText">'+smallTitHtml+
        '</div>'+btnHtml+
        '</div></div>';
    $('.popupBox').html(popupBody);
    popupMask();
    btnClosePopup2();
    //点击确定执行callback
    $('.btn-default2').on('click',function () {
        callBackFun();
        return false;

    });
    if(btnColse){
        $("[data-action='close']").on('click',function () {
            callBackFun();
            return false;
        });
    }
    function callBackFun() {
        if($.isFunction(callback)){
            $('.popupBox').html('');
            $('.popup-mask').hide();
            callback();
        }
    }
    return false;

}


function titPopupState3(tit,context,btnText,callback,noClose) {
	$('.popupBox').html('');
	var bigTit = '',
		btnHtml='',
		smallTitHtml = '',
	  noCloseHtml = '';

	(tit!='') ? bigTit=tit : bigTit = '操作提示';
	smallTitHtml='<div class="popup-body-smallText">'+context+'</div>';

	if(btnText == undefined){
		btnHtml=''
	}else if(typeof (btnText) == 'string'){
		btnHtml='<div class="popup-bodyBtn"><a class="btn btn-line" data-type="1">'+btnText+'</a></div>';
	}else if($.isArray(btnText)){
		btnHtml = '<div class="popup-bodyBtn"><a class="btn btn-line" data-type="2">'+btnText[0]+'</a>'+
			'<a class="btn btn-default2" data-type="3">'+btnText[1]+'</a></div>';
	};

	if(noClose == 'noClose'){
		noCloseHtml = ''
  }else {
    noCloseHtml = '<a class="icon-colseBox" data-type="0"><i class="icon16 icon-colse"></i></a>';
  }

	var popupBody = '';
	popupBody = '<div class="popup smallPopup" style="display: block;">'+
		'<div class="popup-tit"><span>'+bigTit+'</span>'+ noCloseHtml +
		'</div>'+
		'<div class="popup-body">'+
		'<div class="popup-bodyText">'+smallTitHtml+
		'</div>'+btnHtml+
		'</div></div>';
	$('.popupBox').html(popupBody);
	popupMask();
	btnClosePopup2();
	//点击确定执行callback
	$('[data-type]').on('click',function () {
	    if(!callback){
		    closePopup();
      }else {
		    var e=0;
		    e = $(this).attr('data-type');
		    callBackFun(e);
      }
		return false;
	});
	function callBackFun(e) {
		if($.isFunction(callback)){
			closePopup();
			callback(e);
		}
	}
	return false;

}
function closePopup() {
	$('.popupBox').html('');
	$('.popup-mask').hide();
}


